"""
工具8：面试安排工具
功能：创建面试记录
"""
import json
import logging
from langchain.tools import BaseTool
from datetime import datetime

logger = logging.getLogger(__name__)


class ScheduleInterviewTool(BaseTool):
    """面试安排工具"""
    
    name: str = "schedule_interview_tool"
    description: str = """
        安排面试，创建面试记录。
        
        输入参数（JSON字符串）：
        {
            "candidate_id": 123,
            "job_id": 456,
            "interview_time": "2025-01-15 14:00"
        }
        """
    
    def _run(self, tool_input: str) -> str:
        try:
            # 解析输入
            if isinstance(tool_input, str):
                input_dict = json.loads(tool_input)
            elif isinstance(tool_input, dict):
                input_dict = tool_input
            else:
                return json.dumps({"success": False, "error": f"不支持的输入类型: {type(tool_input)}"}, ensure_ascii=False)
            
            candidate_id = input_dict.get('candidate_id')
            job_id = input_dict.get('job_id')
            interview_time = input_dict.get('interview_time')
            
            if not candidate_id or not job_id or not interview_time:
                return json.dumps({"success": False, "error": "必须提供candidate_id、job_id和interview_time"}, ensure_ascii=False)
            
            # 简化实现：记录日志
            logger.info(f"安排面试: candidate={candidate_id}, job={job_id}, time={interview_time}")
            
            # TODO: 实际实现时创建Interview记录
            # from job.models import Interview
            # Interview.objects.create(...)
            
            return json.dumps({
                "success": True,
                "data": {
                    "interview_id": "interview_123",
                    "status": "scheduled"
                },
                "message": "面试安排成功"
            }, ensure_ascii=False)
            
        except json.JSONDecodeError as e:
            logger.error(f"JSON解析失败: {str(e)}")
            return json.dumps({"success": False, "error": f"JSON解析失败: {str(e)}"}, ensure_ascii=False)
        except Exception as e:
            logger.error(f"面试安排失败: {str(e)}")
            return json.dumps({
                "success": False,
                "error": str(e)
            }, ensure_ascii=False)
    
    async def _arun(self, *args, **kwargs):
        raise NotImplementedError()


